class Floder {
    constructor(name) {
        this.name = name;
        this.parent = null
        this.files = []
    }

    add (f) {
        this.files.push(f)
        f.parent = this
    }

    scan() {
        console.log("开始扫描文件夹：",this.name);
        for (let i = 0,file,files = this.files;file = files[i ++ ];) {
            file.scan()
        }
    }

    remove () {
        // if(!this.parent){
        //     return false 
        // }

        // for(let i =0,file,files = this.parent.files;file = files[i ++];){
        //     if(!this.parent){
        //         return 
        //     }
        //     if(file === this) {
        //         this.parent.files.splice(i,1)
        //     }
        // }
        if ( ! this.parent ){    //根节点或者树外的游离节点
            return;
          }
          for ( var files = this.parent.files, l = files.length -1; l >=0; l-- ){
            var file = files[ l ];
            if ( file === this ){
                files.splice( l, 1 );
            }
          }
    }
}
class File {
    constructor(name) {
        this.name = name;
        this.parent = null
    }

    add() {
        throw new Error("文件不能添加文件夹")
    }

    scan() {
        console.log("正在扫描文件：",this.name);
    }
    remove () {
        // if(!this.parent){
        //     return false 
        // }

        // for(let i = 0,file,files = this.parent.files;file = files[i++];){
        //     if(file === this) {
        //         // console.log(this.parent.files,file);
        //         files.splice(i,1)
        //     }
        // }
        if ( ! this.parent ){    //根节点或者树外的游离节点
            return;
          }
          for ( var files = this.parent.files, l = files.length -1; l >=0; l-- ){
            var file = files[ l ];
            if ( file === this ){
                files.splice( l, 1 );
            }
          }
    }
}
const file = new Floder("前端")
const a = new Floder("javascript设计模式与开发实践")
const b = new Floder("node")
const c = new Floder("webpack")
const d = new Floder("vue")
const e = new File("vuejs设计")
const f = new File("node深入浅出")
const g = new File("webpack打包优化")
const h = new File("javascript设计模式")

a.add(h)
b.add(f)
c.add(g)
d.add(e)

file.add(a)
file.add(b)
file.add(c)
file.add(d)

// a.remove()
g.remove()



file.scan()
